package fiuba.td.fabrica.view.elements;

import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import fiuba.td.fabrica.constants.GlobalConstants;
import fiuba.td.fabrica.constants.GlobalConstants.PANTALLA_ACCIONES;
import fiuba.td.fabrica.controller.Controller;
import fiuba.td.fabrica.model.entities.ElementoFabrica;
import fiuba.td.fabrica.model.vos.Posicion;
import fiuba.td.fabrica.view.entities.EntityView;

public class IconoJLabel extends JLabel implements Observer{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String DIRECTORIO_IMG = System.getProperty("user.dir") + "\\recursos\\";
	private static String IMAGEN_DEFECTO = "dirt.jpg"; 
	
	private EntityView entityView;
	private Posicion posicion;
	
	public IconoJLabel(Posicion posicion){
		this.posicion = posicion;
		this.setIcon(new ImageIcon(DIRECTORIO_IMG + IMAGEN_DEFECTO));
	}
	
	public void limpiarEntityView() {
		if (this.entityView != null)
			this.entityView.deleteObserver(this);

		for (MouseListener mouseListener : this.getMouseListeners()) 
			this.removeMouseListener(mouseListener);
		
		this.addMouseListener(Controller.getInstance().getIconoJLabelController(this));
		
		this.setIcon(new ImageIcon(DIRECTORIO_IMG + IMAGEN_DEFECTO));
		this.entityView = null;
	}

	public void setEntityView(EntityView entityView) {
		if (entityView != null){
			this.entityView = entityView;
			this.entityView.setPosicion(this.posicion);	
			this.addMouseListener(this.entityView.getMouseListener());		
			this.entityView.addObserver(this);
			String icon = DIRECTORIO_IMG + this.entityView.getIcon();
			this.setIcon(new ImageIcon(icon));
		}
		else {
			this.setIcon(new ImageIcon(DIRECTORIO_IMG + IMAGEN_DEFECTO));
		}
	}
	
		
	
	public Posicion getPosicion() {
		return posicion;
	}

	public IconoJLabel(EntityView entityView, Posicion posicion)
	{
		this.posicion = posicion;
		
		this.setEntityView(entityView);
	}
	
	public boolean tieneEntityView() {
		return (this.entityView != null);
	}
	
	
	
	public void update(Observable o, Object arg) {
		PANTALLA_ACCIONES accion = (PANTALLA_ACCIONES)arg;
		
		if ((accion == GlobalConstants.PANTALLA_ACCIONES.MOVER) || (accion == GlobalConstants.PANTALLA_ACCIONES.QUITAR_CINTA)) {
			this.limpiarEntityView();
			return;
		} 
		
		if (accion == GlobalConstants.PANTALLA_ACCIONES.DESHABILITAR_EDICION) {
			this.setEnabled(false);
			for (MouseListener mouseListener : this.getMouseListeners()) {
				this.removeMouseListener(mouseListener);
			}

			return;
		}
		
		if (accion == GlobalConstants.PANTALLA_ACCIONES.HABILITAR_EDICION) {
			this.setEnabled(true);
			this.addMouseListener(entityView.getMouseListener());
			return;
		}
		
		
		
		ElementoFabrica elementoFabrica = (ElementoFabrica)o;
		String icon = DIRECTORIO_IMG + elementoFabrica.getIcono();
		this.setIcon(new ImageIcon(icon));
		

	}
	
	public void setMouseListener(MouseListener mouseListener){
		this.addMouseListener(mouseListener);
	}
	
	public JLabel clone (){
		JLabel icono = new IconoJLabel(entityView, posicion);
		icono.setBounds(this.getBounds());
		
		return icono;
	}
}
